package com.xky.email.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.velocity.VelocityEngineUtils;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author keying.xia
 * @date 2017/12/8
 */
@org.springframework.stereotype.Service
public class Service {

    @Autowired
    private JavaMailSender mailSender;


    public void sendEmail(){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("455832573@qq.com");
        message.setTo("455832573@qq.com");
        message.setSubject("主题：简单邮件");
        message.setText("测试邮件内容");

        mailSender.send(message);
    }


    public void sendEmail1() throws Exception{
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("455832573@qq.com");
        helper.setTo("455832573@qq.com");
        helper.setSubject("主题：有附件");
        helper.setText("有附件的邮件");

        FileSystemResource file = new FileSystemResource(new File("D:\\spring\\spring-boot-learning\\email\\src\\main\\resources\\public\\a.html"));
        FileSystemResource file1 = new FileSystemResource(new File("D:\\spring\\spring-boot-learning\\email\\src\\main\\resources\\public\\b.html"));
        helper.addAttachment("附件-1.jpg", file);
        helper.addAttachment("附件-2.jpg", file1);

        mailSender.send(mimeMessage);

    }

    public void sendEmail2() throws Exception{

        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("455832573@qq.com");
        helper.setTo("455832573@qq.com");
        helper.setSubject("主题：嵌入静态资源");
        helper.setText("<html><body><img src=\"cid:weixin\" ></body></html>", true);

        FileSystemResource file = new FileSystemResource(new File("D:\\spring\\spring-boot-learning\\email\\src\\main\\resources\\public\\b.html"));
        helper.addInline("weixin", file);

        mailSender.send(mimeMessage);

    }


}
